package com.zj.osm.view.map.map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;

import com.zj.osm.App;
import com.zj.osm.R;
import com.zj.osm.bean.ReturnLineAndPoint;
import com.zj.osm.entity.db.DaoSession;
import com.zj.osm.entity.db.EquipmentDao;
import com.zj.osm.entity.db.LineDao;
import com.zj.osm.entity.db.PointAndLineBeanDao;
import com.zj.osm.entity.db.PointDao;
import com.zj.osm.entity.db.ProjectDao;
import com.zj.osm.entity.greendao.Equipment;
import com.zj.osm.entity.greendao.Line;
import com.zj.osm.entity.greendao.Point;
import com.zj.osm.entity.greendao.PointAndLineBean;
import com.zj.osm.entity.greendao.Project;
import com.zj.osm.model.pojo.Equip;
import com.zj.osm.utils.db_utils.PointsUtils;
import com.zj.osm.utils.db_utils.LineUtils;
import com.zj.utils.emm.EquipmentCateGoryEmm;
import com.zj.utils.map.MapUtils;
import com.zj.utils.utils.utilcode.ToastUtils;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.overlay.Marker;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.blankj.utilcode.util.StringUtils.getString;
import static com.zj.osm.App.getInstances;
import static com.zj.osm.utils.MyUtils.Distance;

/**
 * MapActivity的数据处理类
 *
 * @author jiang zhu on 2019/10/26
 */
class MapActivityModel {

    private DaoSession daoSession = App.getInstances().getDaoSession();

    Project getProjectById(Long projectId) {
        return daoSession.getProjectDao().queryBuilder().where(ProjectDao.Properties.Id.eq(projectId)).unique();
    }

    List<Line> getLineListById(Long projectId) {
        if (!getInstances().isXinJian) {
            return daoSession.getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(projectId), LineDao.Properties.ParallelPole.notEq("同杆并架"), LineDao.Properties.State.notEq("新建")).list();
        } else {
            return daoSession.getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(projectId), LineDao.Properties.ParallelPole.notEq("同杆并架")).list();

        }
    }

    List<Point> getPointListById(Long projectId) {
        if (!getInstances().isXinJian) {
            return daoSession.getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(projectId), PointDao.Properties.State.notEq("新建")).list();

        } else {
            return daoSession.getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(projectId)).list();

        }
    }

    GeoPoint toPointsLocation(List<Double> latList, List<Double> longList) {
        double maxLat = Collections.max(latList);
        double minLat = Collections.min(latList);
        double maxLong = Collections.max(longList);
        double minLong = Collections.min(longList);
        return new GeoPoint((maxLat + minLat) / 2, (maxLong + minLong) / 2);
    }

    Point addPoint(long id, String icon, IGeoPoint mapCenter) {
        String uid = UUID.randomUUID().toString();
        Point point = new Point();
        point.setProjectId(id);
        point.setPointId(uid);
        point.setUid(uid);
        point.setIcon(icon);
        point.setLatitude(mapCenter.getLatitude());
        point.setLongitude(mapCenter.getLongitude());
        point.setMark(new Random(1000).toString());
        daoSession.insert(point);
        return point;
    }

    @SuppressLint("DefaultLocale")
    Line addLine(Long id, Point point, List<Point> pointList, IGeoPoint mapCenter) {
        Line line = new Line();
        line.setLineId(UUID.randomUUID().toString());
        line.setProjectId(id);
        line.setLength(String.format("%.2f", MapUtils.distance(mapCenter.getLatitude(), mapCenter.getLongitude(), pointList.get(pointList.size() - 1).getLatitude(), pointList.get(pointList.size() - 1).getLongitude())) + "米");
        daoSession.insert(line);
        PointAndLineBean pointAndLineBean = new PointAndLineBean();
        pointAndLineBean.setLineId(line.getId());
        pointAndLineBean.setPointId(point.getId());
        daoSession.insert(pointAndLineBean);
        PointAndLineBean pointAndLineBean1 = new PointAndLineBean();
        pointAndLineBean1.setLineId(line.getId());
        pointAndLineBean1.setPointId(pointList.get(pointList.size() - 1).getId());
        daoSession.insert(pointAndLineBean1);
        return line;
    }

    Line addWithTheLever(Long id, List<String> withTheLeverList) {
        return connect(id, withTheLeverList, true);
    }

    @SuppressLint("DefaultLocale")
    private Line connect(Long id, List<String> withTheLeverList, boolean withTheLever) {
        long id1 = Long.parseLong(withTheLeverList.get(0));
        Point point1 = daoSession.getPointDao().queryBuilder().where(PointDao.Properties.Id.eq(id1)).unique();
        long id2 = Long.parseLong(withTheLeverList.get(1));
        Point point2 = daoSession.getPointDao().queryBuilder().where(PointDao.Properties.Id.eq(id2)).unique();

        List<Line> lineLista = new LinkedList<>();

        List<Line> lineList = daoSession.getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(id)).build().list();

        for (int i = 0; i < lineList.size(); i++) {
            if (lineList.get(i).getPointList().get(0).getLatitude() == point1.getLatitude() && lineList.get(i).getPointList().get(1).getLatitude() == point2.getLatitude() || lineList.get(i).getPointList().get(1).getLatitude() == point1.getLatitude() && lineList.get(i).getPointList().get(0).getLatitude() == point2.getLatitude()) {
                lineLista.add(lineList.get(i));
            }
        }
        if (!withTheLever && lineLista.size() > 0) {
            ToastUtils.showShort("两点之间已有线");
            return null;
        }
        if (lineLista.size() > 2) {
            ToastUtils.showShort("同杆并架最多只能有三根线");
            return null;
        } else {
            Line line = new Line();
            if (lineLista.size() > 0) {
                line.setMethodName(String.valueOf(lineLista.size()));
                line.setParallelPole("同杆并架");
            }
            line.setLineId(UUID.randomUUID().toString());
            line.setProjectId(id);
            line.setLength(String.format("%.2f", MapUtils.distance(point1.getLatitude(), point1.getLongitude(), point2.getLatitude(), point2.getLongitude())) + "米");
            daoSession.insert(line);
            PointAndLineBean pointAndLineBean = new PointAndLineBean();
            pointAndLineBean.setLineId(line.getId());
            pointAndLineBean.setPointId(point1.getId());
            daoSession.insert(pointAndLineBean);
            PointAndLineBean pointAndLineBean1 = new PointAndLineBean();
            pointAndLineBean1.setLineId(line.getId());
            pointAndLineBean1.setPointId(point2.getId());
            daoSession.insert(pointAndLineBean1);
            return line;
        }
    }


    List<Line> getWithTheLeverLineListById(Long projectId) {
        List<Line> allLine = daoSession.getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(projectId), LineDao.Properties.ParallelPole.eq("同杆并架")).list();
        Log.e("alll", allLine.toString());
        return allLine;
    }

    void deleteLine(String lineId) {
        long lineID = Long.parseLong(lineId);
        daoSession.getLineDao().deleteByKey(lineID);
        daoSession.queryBuilder(PointAndLineBean.class).where(PointAndLineBeanDao.Properties.LineId.eq(lineID)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    Point deletePoint(String pointId) {
        long pointID = Long.parseLong(pointId);
        Point point = daoSession.getPointDao().queryBuilder().where(PointDao.Properties.Id.eq(pointID)).unique();

        return point;
    }

    List<Line> deletePointAndLine(Point point) {
        List<Line> lineList = point.getLineList();
        if (lineList != null && lineList.size() > 0) {
            for (int i = 0; i < lineList.size(); i++) {
                daoSession.getLineDao().delete(lineList.get(i));
            }
        }
        daoSession.getPointDao().delete(point);
        return lineList;
    }

    Line connectionLine(Long id, List<String> connectionLine) {
        return connect(id, connectionLine, false);
    }

    Point continuingSurvey(String id) {
        return daoSession.getPointDao().queryBuilder().where(PointDao.Properties.Id.eq(Long.parseLong(id))).unique();
    }


    /**
     * 这里处理杆塔和设备的兴建逻辑。
     */
    public ReturnLineAndPoint addMarkerAndLine(Context context, Project project, Integer isTfenjieState, boolean WHETHER_TO_SHOW_GUIDES, double high, String address, Marker currentMarker, IGeoPoint mapCenter, EquipmentCateGoryEmm equipmentCateGoryEmm) {
        final Equip[] equip = new Equip[1];
        if (equipmentCateGoryEmm.equals(EquipmentCateGoryEmm.NEWTOWER)) {
            //杆塔
            final List<Equip> twoerList = PointsUtils.Companion.getEquIp(context, 1);
            equip[0] = twoerList.get(0);
        } else if (equipmentCateGoryEmm.equals(EquipmentCateGoryEmm.NEWGONEJINE)) {
            //设备
            final List<Equip> euipList = PointsUtils.Companion.getEquIp(context, 2);
            equip[0] = euipList.get(2);

        } else {
            //设备
            final List<Equip> euipList = PointsUtils.Companion.getEquIp(context, 2);
            equip[0] = euipList.get(1);

        }


        /**-----------------------------------------  点的操作  -------------------------Start-----------------------------------**/
        int lengths = 0;
        Point points = new Point();
        points.setId(null);
        points.setUid(getUUID());
        points.setPointId(getUUID());
        points.setProjectId(project.getId());
        points.setLegendTypeKey(equip[0].getRemark());
        points.setNote("");
        points.setAddress(address);
        points.setLatitude(mapCenter.getLatitude());
        points.setLongitude(mapCenter.getLongitude());
        points.setBatch("0");
        points.setDefaultLineType(equip[0].getDefaultLineType());
        if (isTfenjieState != 0)
            points.setIsT("");
        points.setEquipType_id(equip[0].getId());
        points.setSblx(equip[0].getName());
        List<Point> pointsList11 = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId())).list();
        int sizeInt = 0;
        Line intentLine = null;
        if (pointsList11.size() > 0) {
            String regEx = "[0-9]+(?=[^0-9]*$)";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(pointsList11.get(pointsList11.size() - 1).getMark());
            while (m.find()) {
                //默认不继承T分阶
                points.setIsT("");
                points.setMark(pointsList11.get(pointsList11.size() - 1).getMark().replaceAll(regEx, (Integer.parseInt(Objects.requireNonNull(m.group(0))) + 1) + ""));
            }
        } else {
            points.setMark(equip[0].getCode() + "-" + (sizeInt + 1));
        }
        points.setHigh((String.valueOf(high)).replace("4.9E-324", "0"));
        BigDecimal bg;
        Point crrentMartPoints = null;
        if (currentMarker != null) {
            crrentMartPoints = PointsUtils.Companion.getCurrentPoint(currentMarker.getId(), project.getId());
            //获取glsb肯定是第一个和当前点需要连接的点。就是
            points.setGlsb(crrentMartPoints.getMark());
            bg = new BigDecimal(Distance(mapCenter.getLatitude(), mapCenter.getLongitude(), crrentMartPoints.getLatitude(), crrentMartPoints.getLongitude())).setScale(0, RoundingMode.UP);
            if (Distance(mapCenter.getLatitude(), mapCenter.getLongitude(), crrentMartPoints.getLatitude(), crrentMartPoints.getLongitude()) < 0.6) {
                com.blankj.utilcode.util.ToastUtils.showShort("请重新点位，距离太近");
                return null;
            }
            lengths = bg.intValue();
        }
        //杆塔/设备
        points.setName(EquipmentCateGoryEmm.NEWTOWER.equals(equipmentCateGoryEmm) ? getString(R.string.map_tower) : getString(R.string.map_equipment));
        points.setEquipType_name(equip[0].toString().trim());
        if (getInstances().isXinJian) {
            points.setState("新建");

        } else {
            points.setState("原有");

        }
        if (crrentMartPoints == null) {
            points.setMark(equip[0].getCode() + "-" + (sizeInt + 1));

        } else {
            if (isTfenjieState == 1) {//表示T出来的。
                //points.setMark(equip[0].getCode() + "-" + PointsUtils.Companion.getInPointsMaxNumber(project.getId(), points.getName()));
                points.setMark(crrentMartPoints.getMark()+"-1");
            } else {
//                String strNewMark = PointsUtils.Companion.getGlLine_bianhao(crrentMartPoints, points, equip[0].getCode() + "-" + (sizeInt + 1), project.getId(), equip[0].getCode(), context);
//                points.setMark(strNewMark);
            }


        }
        points.setIcon(equip[0].getIcon());
        points.setWhether("0");
        points.setDangju(lengths);

        List<Point> hunanDlanjin = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId()),PointDao.Properties.EquipType_name.eq("电缆井")).list();
        List<Point> hunanHwx = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId()),PointDao.Properties.EquipType_name.eq("环网箱")).list();

        //region 湖南
        if(points.getEquipType_name().equals("电缆井")&&!hunanDlanjin.isEmpty()){
            Point huNanPoint= hunanDlanjin.get(hunanDlanjin.size()-1);
            //如果上一个和当前的equipType一样就继承
            if(huNanPoint.getEquipType_name().equals(points.getEquipType_name())){
                if(huNanPoint.getRoadName()!=null){
                    points.setRoadName(huNanPoint.getRoadName());
                }
                //性质
                if (huNanPoint.getCabwellPy()!=null) {
                    points.setCabwellPy(huNanPoint.getCabwellPy());
                }
                //资源状态
                if (huNanPoint.getRsState()!=null) {
                    points.setRsState(huNanPoint.getRsState());
                }
                //规划年
                if (huNanPoint.getGhYear()!=null){
                    points.setGhYear(huNanPoint.getGhYear());
                }
            }
        }else if(points.getEquipType_name().equals("环网箱")&&!hunanHwx.isEmpty()){
            Point huNanPoint= hunanHwx.get(hunanHwx.size()-1);
            if(huNanPoint.getRoadName()!=null){
                points.setRoadName(huNanPoint.getRoadName());
            }
            //性质
            if (huNanPoint.getCabwellPy()!=null) {
                points.setCabwellPy(huNanPoint.getCabwellPy());
            }
            //设置道路性质
            if (huNanPoint.getRoadNature()!=null) {
                points.setRoadNature(huNanPoint.getRoadNature());
            }
            //设置资源规格
            if (huNanPoint.getReSpecfs()!=null) {
                points.setReSpecfs(huNanPoint.getReSpecfs());
            }
            //资源状态
            if (huNanPoint.getRsState()!=null) {
                points.setRsState(huNanPoint.getRsState());
            }
            //规划年
            if (huNanPoint.getGhYear()!=null){
                points.setGhYear(huNanPoint.getGhYear());
            }
        }
        //enregion 湖南




        Long id = App.getInstances().getDaoSession().getPointDao().insert(points);
        Equipment equipment = PointsUtils.Companion.getGlLine_ganxin(project, crrentMartPoints, points, context);
        if (equipment != null) {
            List<Equipment> eqs = getInstances().getDaoSession().getEquipmentDao().queryBuilder().where(EquipmentDao.Properties.PointId.eq(points.getId()), EquipmentDao.Properties.Qubie.eq(0)).build().list();
            if (eqs == null || eqs.size() == 0) {
                Equipment insertEquipment = new Equipment();
                insertEquipment.setId(null);
                insertEquipment.setName(equipment.getName());
                insertEquipment.setTwoName(equipment.getTwoName());
                insertEquipment.setThreeName(equipment.getThreeName());
                insertEquipment.setFourName(equipment.getFourName());
                insertEquipment.setQubie(equipment.getQubie());
                insertEquipment.setNote(equipment.getNote());
                insertEquipment.setPointId(id);
                insertEquipment.setAmount(equipment.getAmount());
                insertEquipment.setLever_id(equipment.getLever_id());
                insertEquipment.setHead_id(equipment.getHead_id());
                insertEquipment.setThreeId(equipment.getThreeId());
                insertEquipment.setFourId(equipment.getFourId());
                insertEquipment.setLineName(equipment.getLineName());
                insertEquipment.setModule_code(equipment.getModule_code());
                insertEquipment.setModuleID(equipment.getModuleID());
                insertEquipment.setModuleTxt(equipment.getModuleTxt());
                insertEquipment.setProjectId(equipment.getProjectId());
                insertEquipment.setModuleTxt(equipment.getModuleTxt());
                getInstances().getDaoSession().getEquipmentDao().insert(insertEquipment);
            }
        }

        /**-----------------------------------------  点的操作  -------------------------End-----------------------------------**/


        /**-----------------------------------------  线的操作  -------------------------Start-----------------------------------**/

        /**
         * 可以分装
         * 如果上一个点是T分阶那么我们
         * //点A的经度  是否T分支 如果是  为1 ,主干点 "" 十字接 2
         */
        List<Line> gulianLine = new ArrayList<>();
        //1。如果端头一定可以看见。
        //这里判断如果线不是T线且连点有2条那么就隐藏其他的可以进行显示。
        List<Point> allguanLianPoints = new ArrayList<>();
        if (crrentMartPoints != null) {
            List<Line> allguanLian = getInstances().getDaoSession().getLineDao().queryBuilder().whereOr(LineDao.Properties.Start_id.eq(crrentMartPoints.getUid()), LineDao.Properties.End_id.eq(crrentMartPoints.getUid())).list();
            for (int i = 0; i < allguanLian.size(); i++) {
                List<Point> pointLists = getInstances().getDaoSession().getPointDao().queryBuilder().whereOr(PointDao.Properties.Uid.eq(allguanLian.get(i).getStart_id()), PointDao.Properties.Uid.eq(allguanLian.get(i).getEnd_id())).list();
                allguanLianPoints.addAll(pointLists);
            }
            allguanLianPoints = new ArrayList(new HashSet(allguanLianPoints));
            if (allguanLianPoints.size() == 2) {//如果是有切只有一个关联点那么一定全部继承。
                gulianLine = getInstances().getDaoSession().getLineDao().queryBuilder().whereOr(LineDao.Properties.End_id.eq(crrentMartPoints.getUid()), LineDao.Properties.Start_id.eq(crrentMartPoints.getUid())).build().list();

            } else if (allguanLianPoints.size() > 2) {
                List<Line> allGuanlianLine = getInstances().getDaoSession().getLineDao().queryBuilder().whereOr(LineDao.Properties.Start_id.eq(crrentMartPoints.getUid()), LineDao.Properties.End_id.eq(crrentMartPoints.getUid())).whereOr(LineDao.Properties.ParallelPole.isNull(), LineDao.Properties.ParallelPole.eq("0"), LineDao.Properties.ParallelPole.eq("同杆并架")).list();
                List<Line> unitqGuanlianLine = new ArrayList<>();
                List<String> unitqGuanlianLineName = new ArrayList<>();
                for (int i = 0; i < allGuanlianLine.size(); i++) {
                    if (unitqGuanlianLineName.contains(allGuanlianLine.get(i).getName())) {
                        unitqGuanlianLineName.remove(allGuanlianLine.get(i).getName());
                        for (int j = 0; j < unitqGuanlianLine.size(); j++) {
                            if (unitqGuanlianLine.get(j).getName().equals(allGuanlianLine.get(i).getName())) {
                                unitqGuanlianLine.remove(unitqGuanlianLine.get(j));
                            }
                        }
                    } else {
                        unitqGuanlianLineName.add(allGuanlianLine.get(i).getName());
                        unitqGuanlianLine.add(allGuanlianLine.get(i));
                    }
                }
                unitqGuanlianLineName.clear();
                gulianLine.clear();
                gulianLine.addAll(unitqGuanlianLine);
            }

        }//分装线的为工具方法


        if (WHETHER_TO_SHOW_GUIDES && crrentMartPoints != null && pointsList11.size() > 0) {
            String LineType = "";
            if (crrentMartPoints.getDefaultLineType() != null) {
                if (crrentMartPoints.getDefaultLineType().equals("1") && points.getDefaultLineType().equals("1")) {
                    LineType = "架空线路";
                } else {
                    LineType = "电缆";
                }
            } else {
                if (LineUtils.getInstance().getDefaultLineType(crrentMartPoints.getSblx()).equals("1") && points.getDefaultLineType().equals("1")) {
                    LineType = "架空线路";
                } else {
                    LineType = "电缆";
                }
            }

            //TODO 这里是主线必须为0
            if (gulianLine.size() > 0 && isTfenjieState == 0 && gulianLine.get(0).getType() != null) {
                for (Line l : gulianLine) {
                    Line line = new Line();
                    line.setCid(PointsUtils.Companion.getInstance().getUUID());
                    line.setProjectId(project.getId());
                    line.setItem_id(project.getId());
                    line.setName(l.getName());
                    line.setLatB(points.getLatitude() + "");
                    line.setLonB(points.getLongitude() + "");
                    line.setLatA(currentMarker.getPosition().getLatitude() + "");
                    line.setLonA(currentMarker.getPosition().getLongitude() + "");
                    if (crrentMartPoints.getDefaultLineType() != null) {
                        if (crrentMartPoints.getDefaultLineType().equals("1") && points.getDefaultLineType().equals("1")) {
                            line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压架空线路"));
                            line.setType("架空线路");
                            line.setTypeId("11");
                        } else {
                            line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压电缆线路"));
                            line.setType("电缆");
                            line.setTypeId("12");
                        }

                    } else {
                        if (LineUtils.getInstance().getDefaultLineType(crrentMartPoints.getSblx()).equals("1") && points.getDefaultLineType().equals("1")) {
                            line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压架空线路"));
                            line.setType("架空线路");
                            line.setTypeId("11");
                        } else {
                            line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压电缆线路"));
                            line.setType("电缆");
                            line.setTypeId("12");
                        }
                    }

                    Log.e("linessThpeke===", line.toString());
                    line.setStart_id(crrentMartPoints.getUid());
                    line.setEnd_id(points.getUid());
                    Log.e("inllllLegth==", lengths + "");
                    line.setLength(lengths + "");


                    //选型
                    line.setBackNum("1");
                    if (l.getModuleID() != null) {
                        if (l.getType().contains(line.getType())) {//类型相同的。
                            line.setNum(l.getNum());
                            line.setModuleID(l.getModuleID());
                            line.setModuleTxt(l.getModuleTxt());
                            line.setTypes(l.getTypes());
                            line.setTypeId(l.getTypeId());
                            line.setType(l.getType());
                            line.setMethodName(l.getMethodName());
                            line.setPath(l.getPath());

                            if (l.getRoadPands()!=null){
                                line.setRoadPands(l.getRoadPands());
                            }
                            if (l.getRoadAdress()!=null){
                                line.setRoadAdress(l.getRoadAdress());
                            }
                            if (l.getArdUseNumer()!=null){
                                line.setArdUseNumer(l.getArdUseNumer());
                            }
                            if (l.getElectriHoleNumer()!=null){
                                line.setElectriHoleNumer(l.getElectriHoleNumer());
                            }
                            if (l.getRemaNumber()!=null){
                                line.setRemaNumber(l.getRemaNumber());
                            }
                            if (l.getSrcYear()!=null){
                                line.setSrcYear(l.getSrcYear());
                            }




                            if (l.getParallelPole() != null && l.getParallelPole().equals("同杆并架")) {
                                line.setParallelPole(l.getParallelPole());
                                if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                    line.setLegendTypeKey(l.getLegendTypeKey());
                                }
                            } else {
                                line.setParallelPole("0");
                                //如果选型关联的线1的legendTypeKey有关联的线的选型。高低压之类的我们也需要继承。
                                if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                    line.setLegendTypeKey(l.getLegendTypeKey());
                                    //回 滚
                                }
                            }
                            line.setState(l.getState());
                        } else {//类型不同的线寻找关联的。
                            Line defaultLine = LineUtils.getInstance().getGline_xianlu(line, project);
                            line.setNum(l.getNum());
                            line.setModuleID(defaultLine.getModuleID());
                            line.setModuleTxt(defaultLine.getModuleTxt());
                            line.setTypes(defaultLine.getTypes());
                            line.setTypeId(defaultLine.getTypeId());
                            if (defaultLine.getType() != null && !defaultLine.getType().isEmpty()) {
                                line.setType(defaultLine.getType());
                            }
                            line.setMethodName(defaultLine.getMethodName());
                            line.setPipe_laying_state("新建");
                            line.setPath(defaultLine.getPath());
                            if (l.getParallelPole() != null && l.getParallelPole().equals("同杆并架")) {
                                line.setParallelPole(l.getParallelPole());
                                if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                    line.setLegendTypeKey(l.getLegendTypeKey());
                                }
                            } else {
                                line.setParallelPole("0");
                            }
                            line.setState(l.getState());
                        }

                    } else {
                        if (getInstances().isXinJian) {
                            line.setState("新建");

                        } else {
                            line.setState("原有");
                        }
                        Line defaultLine = LineUtils.getInstance().getGline_xianlu(line, project);
                        if (defaultLine != null && defaultLine.getModuleID() != null && !defaultLine.getModuleID().isEmpty()) {
                            line.setNum("1");
                            line.setModuleID(defaultLine.getModuleID());
                            line.setModuleTxt(defaultLine.getModuleTxt());
                            line.setTypes(defaultLine.getTypes());
                            line.setTypeId(defaultLine.getTypeId());
                            line.setType(defaultLine.getType());
                            line.setMethodName(defaultLine.getMethodName());
                            line.setPipe_laying_state("新建");
                            line.setPath(defaultLine.getPath());
                            if (l.getParallelPole() != null && l.getParallelPole().equals("同杆并架")) {
                                line.setParallelPole(l.getParallelPole());
                                if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                    line.setLegendTypeKey(l.getLegendTypeKey());
                                }
                            } else {
                                line.setParallelPole("0");
                                //如果选型关联的线1的legendTypeKey有关联的线的选型。高低压之类的我们也需要继承。
                                if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                    if (l.getType().contains(line.getType())) {//类型相同的。
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                    }
                                }
                            }
                        } else {
                            if (l.getParallelPole() != null && !l.getParallelPole().isEmpty()) {
                                if (l.getParallelPole().equals("同杆并架")) {
                                    line.setParallelPole(l.getParallelPole() + "");//如果是1 表示 T分支路线，如果是2表示十字街路线。其他主线路
                                    if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                    }
                                } else {
                                    line.setParallelPole(isTfenjieState + "");//如果是1 表示 T分支路线，如果是2表示十字街路线。其他主线路
                                    line.setIsT(isTfenjieState + "");//如果是1 表示 T分支路线，如果是2表示十字街路线。其他主线路
                                    if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                    }
                                }
                            } else {
                                line.setParallelPole(isTfenjieState + "");
                                line.setIsT(isTfenjieState + "");//如果是1 表示 T分支路线，如果是2表示十字街路线。其他主线路
                                if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                    if (l.getType().contains(line.getType())) {//类型相同的。
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                    }
                                }
                            }
                        }

                    }
                    if (WHETHER_TO_SHOW_GUIDES) {
                        Long LineID = getInstances().getDaoSession().getLineDao().insert(line);
                        intentLine = line;
                        //TODO 这里处理画线
                        //drawMyRoute(line, btn_Length_isCheck);
                        return new ReturnLineAndPoint(line, points, equip[0].getId());
                    }
                }
            } else {
                //TODO T出来的线和
                String fenzhi_LineName = "";
                final List<Point> pointsList121 = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId()), PointDao.Properties.Whether.eq("0")).build().list();
                if (pointsList121.size() > 0 && !equip[0].getDefaultLineType().trim().equals("0")) {
                    Line line = new Line();
                    List<Line> lineList = getInstances().getDaoSession().getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(project.getId())).build().list();
                    line.setId(null);
                    line.setItem_id(project.getId());
                    line.setProjectId(project.getId());
                    line.setCid(PointsUtils.Companion.getInstance().getUUID());
                    line.setLatB(points.getLatitude() + "");
                    line.setLonB(points.getLongitude() + "");
                    line.setLatA(currentMarker.getPosition().getLatitude() + "");
                    line.setLonA(currentMarker.getPosition().getLongitude() + "");
                    line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压架空线路"));
                    line.setStart_id(crrentMartPoints.getUid());
                    line.setEnd_id(points.getUid());
                    line.setLength(lengths + "");
                    line.setBackNum("1");
                    if (crrentMartPoints.getIsT() != null && !crrentMartPoints.getIsT().isEmpty() && isTfenjieState != 0) {
                        line.setParallelPole(isTfenjieState + "");
                        line.setIsT(crrentMartPoints.getIsT());//如果是1 表示 T分支路线，如果是2表示十字街路线。其他主线路
                    } else {
                        line.setParallelPole(String.valueOf(isTfenjieState));//如果是1 表示 T分支路线，如果是2表示十字街路线。其他主线路

                    }
                    if ((crrentMartPoints.getName().equals("杆塔") || crrentMartPoints.getSblx().equals("柱上变压器")) && points.getName().equals("杆塔")) {
                        //TODO 这里修改了    line.setType("架空线路");
                        line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压架空线路"));
                        line.setType("架空线路");
                        line.setTypeId("11");
                        if (lineList.size() > 0) {
                            String line_names = "线路" + LineUtils.getInstance().getInLinesMaxNumber(project.getId()) + "#";
                            //这里获取所有的线路中的线。且找出线的最大的数字并加1即可
                            line.setName(line_names);
                        } else {
                            line.setName("线路" + 1 + "#");
                        }
                    } else {
                        line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压电缆线路"));
                        line.setType("电缆");
                        line.setTypeId("12");
                        if (lineList.size() > 0) {
                            //int mad = (int) (lineList.get(lineList.size() - 1).getId() + 1);
                            String line_names = "线路" + LineUtils.getInstance().getInLinesMaxNumber(project.getId()) + "#";
                            line.setName(line_names);
                        } else {
                            line.setName(1 + "#");
                        }
                    }
                    if (getInstances().isXinJian) {
                        line.setState("新建");

                    } else {
                        line.setState("原有");
                    }
                    line.setNum("1");
                    Line defaultLine = LineUtils.getInstance().getGline_xianlu(line, project);
                    if (isTfenjieState == 0 && defaultLine != null && defaultLine.getModuleID() != null && !defaultLine.getModuleID().equals("null") && !defaultLine.getModuleID().isEmpty()) {
                        line.setNum("1");
                        line.setModuleID(defaultLine.getModuleID());
                        line.setModuleTxt(defaultLine.getModuleTxt());
                        line.setTypes(defaultLine.getTypes());
                        line.setTypeId(defaultLine.getTypeId());
                        line.setType(defaultLine.getType());
                        line.setMethodName(defaultLine.getMethodName());
                        line.setPipe_laying_state("新建");
                        line.setPath(defaultLine.getPath());
                    }
                    //TODO 保存线
                    if (WHETHER_TO_SHOW_GUIDES) {
                        getInstances().getDaoSession().getLineDao().insert(line);
                        intentLine = line;
                        //TODO 调用去view层画线
                        //drawMyRoute(line, btn_Length_isCheck);
                        return new ReturnLineAndPoint(line, points, equip[0].getId());
                        //TODO 这里是第一次关闭状态所以
                    }
                }
            }
        }

        return new ReturnLineAndPoint(intentLine, points, equip[0].getId());

    }

    public ReturnLineAndPoint addMarkerAndLineTopy(EquipmentCateGoryEmm equipmentCateGoryEmm, Context context, Project project, boolean jixukance, boolean WHETHER_TO_SHOW_GUIDES, double high, String address, Marker currentMarker, IGeoPoint mapCenter, Point jpoints) {
        final List<Equip> equips = PointsUtils.Companion.getEquIp(context, 4);
        ReturnLineAndPoint returnLineAndPoint = null;
        long id = -1;
        //地形点排序
        Collections.sort(equips, (o1, o2) -> Integer.compare(o2.getId(), o1.getId()));
        List<Point> pointsList = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId()), PointDao.Properties.State.notEq("新建")).build().list();
        String glsb = "";//关联设备
        if (pointsList.size() > 0) {
            if (!pointsList.get(pointsList.size() - 1).getName().equals("通讯线")) {
                if (jixukance) {
                    glsb = jpoints.getMark();
                } else {
                    glsb = pointsList.get(pointsList.size() - 1).getMark();
                }
            } else {
                glsb = "";
            }
        }
        final Equip[] ep = new Equip[1];
        ep[0] = equips.get(0);
        /**
         * 2019年10月21日。修改内容
         */
        double lengths = 0;
        Point points = new Point();
        points.setId(null);
        points.setUid(getUUID());
        points.setProjectId(project.getId());
        points.setLegendTypeKey(ep[0].getRemark());
        points.setNote("");
        points.setLatitude(mapCenter.getLatitude());
        points.setLongitude(mapCenter.getLongitude());
        points.setBatch("0");
        points.setSblx(ep[0].getName());
        points.setGlsb(ep[0].getCode());
        points.setAddress(address);
        points.setName("地形点");
        points.setDefaultLineType(ep[0].getDefaultLineType());
        points.setEquipType_id(ep[0].getId());
        List<Point> pointsList10 = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId())).orderDesc(PointDao.Properties.Id).list();
        int sizeInt = 0;
        if (pointsList10.size() > 0) {
            Point crrentMartPoints = (Point) currentMarker.getRelatedObject();
            String regEx = "[0-9]+(?=[^0-9]*$)";
            Pattern p = Pattern.compile(regEx);
            Matcher m = p.matcher(pointsList10.get(pointsList10.size() - 1).getMark());
            while (m.find()) {
                //默认不继承T分阶
                points.setIsT("");
                String strNewMark = PointsUtils.Companion.getGlLine_bianhao(crrentMartPoints, points, ep[0].getCode() + "-" + (sizeInt + 1), project.getId(), ep[0].getCode(), context);
                points.setMark(strNewMark);
            }
        } else {
            points.setMark(ep[0].getCode() + "-" + (sizeInt + 1));
        }
        //设置是否为拐点和连接点。
        points.setWhether(ep[0].getComponent());
        points.setIcon(ep[0].getIcon());

        points.setHigh((String.valueOf(high)).replace("4.9E-324", ""));
        points.setLength("");
        points.setWidth("");

        points.setEquipType_name(ep[0].getName());
        if (getInstances().isXinJian) {
            points.setState("新建");
        } else {
            points.setState("原有");
        }
        //TODO 关联关系
        //TODO Whether是否为地形点 1代表是地形点吧但是这里如果是地形拐点就需要搞定为0
        if (points.getSblx().contains("拐点") || points.getSblx().contains("连接点") || points.getSblx().contains("下户拐点")) {
            WHETHER_TO_SHOW_GUIDES = true;
            points.setWhether("0");
            if (glsb != null && !glsb.equals("")) {
                points.setGlsb(glsb);
            } else {
                points.setGlsb(glsb);

            }
            /**
             * 如果上一个点是T分阶那么我们
             * //点A的经度  是否T分支 如果是  为1 ,主干点 "" 十字接 2
             */
            List<Line> gulianLine = new ArrayList<>();
            //1。如果端头一定可以看见。
            //这里判断如果线不是T线且连点有2条那么就隐藏其他的可以进行显示。
            List<Point> allguanLianPoints = new ArrayList<>();
            Point crrentMartPoints = null;
            if (currentMarker != null) {
                crrentMartPoints = (Point) currentMarker.getRelatedObject();
            }
            if (crrentMartPoints != null) {
                List<Line> allguanLian = getInstances().getDaoSession().getLineDao().queryBuilder().whereOr(LineDao.Properties.Start_id.eq(crrentMartPoints.getUid()), LineDao.Properties.End_id.eq(crrentMartPoints.getUid())).list();
                Log.e("allLine=", allguanLian.toString());
                for (int i = 0; i < allguanLian.size(); i++) {
                    List<Point> pointLists = getInstances().getDaoSession().getPointDao().queryBuilder().whereOr(PointDao.Properties.Uid.eq(allguanLian.get(i).getStart_id()), PointDao.Properties.Uid.eq(allguanLian.get(i).getEnd_id())).list();
                    allguanLianPoints.addAll(pointLists);
                }
                allguanLianPoints = new ArrayList(new HashSet(allguanLianPoints));
                if (allguanLianPoints.size() == 2) {//如果是有切只有一个关联点那么一定全部继承。
                    gulianLine = getInstances().getDaoSession().getLineDao().queryBuilder().whereOr(LineDao.Properties.End_id.eq(crrentMartPoints.getUid()), LineDao.Properties.Start_id.eq(crrentMartPoints.getUid())).build().list();

                } else if (allguanLianPoints.size() > 2) {
                    List<Line> allGuanlianLine = getInstances().getDaoSession().getLineDao().queryBuilder().whereOr(LineDao.Properties.Start_id.eq(crrentMartPoints.getUid()), LineDao.Properties.End_id.eq(crrentMartPoints.getUid())).whereOr(LineDao.Properties.ParallelPole.isNull(), LineDao.Properties.ParallelPole.eq("0"), LineDao.Properties.ParallelPole.eq("同杆并架")).list();
                    List<Line> unitqGuanlianLine = new ArrayList<>();
                    List<String> unitqGuanlianLineName = new ArrayList<>();
                    for (int i = 0; i < allGuanlianLine.size(); i++) {
                        if (unitqGuanlianLineName.contains(allGuanlianLine.get(i).getName())) {
                            unitqGuanlianLineName.remove(allGuanlianLine.get(i).getName());
                            for (int j = 0; j < unitqGuanlianLine.size(); j++) {
                                if (unitqGuanlianLine.get(j).getName().equals(allGuanlianLine.get(i).getName())) {
                                    unitqGuanlianLine.remove(unitqGuanlianLine.get(j));
                                }
                            }
                        } else {
                            unitqGuanlianLineName.add(allGuanlianLine.get(i).getName());
                            unitqGuanlianLine.add(allGuanlianLine.get(i));
                        }
                    }
                    unitqGuanlianLineName.clear();
                    gulianLine.clear();
                    gulianLine.addAll(unitqGuanlianLine);
                }

            }


            final List<Point> pointsList12 = getInstances().getDaoSession().getPointDao().queryBuilder().where(PointDao.Properties.ProjectId.eq(project.getId())).build().list();
            if (pointsList12.size() > 0) {
                //获取 LegendTypeKey 默认固定的。
                //Points crrentMartPoints = currentMarker.getExtraInfo().getParcelable("point");
                BigDecimal bg = new BigDecimal(Distance(Double.valueOf(mapCenter.getLatitude() + ""), Double.valueOf(mapCenter.getLongitude() + ""), pointsList12.get(pointsList12.size() - 1).getLatitude(), pointsList12.get(pointsList12.size() - 1).getLongitude())).setScale(2, RoundingMode.UP);
                if (Distance(Double.valueOf(mapCenter.getLatitude() + ""), Double.valueOf(mapCenter.getLongitude() + ""), pointsList12.get(pointsList12.size() - 1).getLatitude(), pointsList12.get(pointsList12.size() - 1).getLongitude()) < 0.6) {
                    com.blankj.utilcode.util.ToastUtils.showShort("移动距离太近，请重新选择移动位置");
                    returnLineAndPoint = new ReturnLineAndPoint(null, points, ep[0].getId());
                }
                Log.e("distance", bg.doubleValue() + "");
                lengths = bg.doubleValue();
                List<Line> lineList = getInstances().getDaoSession().getLineDao().queryBuilder().where(LineDao.Properties.ProjectId.eq(project.getId())).whereOr(LineDao.Properties.Start_id.eq(crrentMartPoints.getUid()), LineDao.Properties.End_id.eq(crrentMartPoints.getUid())).build().list();
                //这里是连接点关联部分
                if (gulianLine.size() > 0) {//如果有关联线1
                    for (Line l : gulianLine) {
                        Line line = new Line();
                        line.setId(null);
                        //line.setLineNumber(1);
                        line.setCid(getUUID());
                        line.setItem_id(project.getId());
                        line.setProjectId(project.getId());
                        line.setName(l.getName());
                        line.setParallelPole("0");
                        line.setLatB(points.getLatitude() + "");
                        line.setLonB(points.getLongitude() + "");
                        line.setLatA(currentMarker.getPosition().getLatitude() + "");
                        line.setLonA(currentMarker.getPosition().getLongitude() + "");
                        line.setStart_id(crrentMartPoints.getUid());
                        //这里肯定是高压电缆线路因为。是连接点和电缆拐点。
                        line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压电缆线路"));
                        line.setEnd_id(points.getUid());
                        line.setLength(lengths + "");
                        line.setBackNum("1");
                        if ((crrentMartPoints.getName().equals("杆塔") || crrentMartPoints.getName().equals("柱上变压器")) && points.getName().equals("杆塔")) {
                            //TODO 这里修改了                        line.setType("架空线路");
                            line.setType("");
                            line.setTypeId("11");
                       /* String line_names="线路"+LineUtils.getInstance().getInLinesMaxNumber(cid)+"#";
                        line.setName(line_names);*/
                        } else {
                            line.setType("电缆");
                            line.setTypeId("12");
                        /*String line_names="线路"+LineUtils.getInstance().getInLinesMaxNumber(cid)+"#";
                        line.setName(line_names);*/
                        }
                        line.setState("新建");
                        line.setNum("1");
                        //选型
                        line.setBackNum("1");
                        if (l.getModuleID() != null && !l.getModuleID().isEmpty()) {
                            if (l.getType().contains(line.getType())) {//类型相同的。
                                line.setNum(l.getNum());
                                line.setModuleID(l.getModuleID());
                                line.setModuleTxt(l.getModuleTxt());
                                line.setTypes(l.getTypes());
                                line.setTypeId(l.getTypeId());
                                line.setType(l.getType());
                                line.setMethodName(l.getMethodName());
                                line.setPath(l.getPath());
                                if (l.getParallelPole() != null && l.getParallelPole().equals("同杆并架")) {
                                    line.setParallelPole(l.getParallelPole());
                                    if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                    }
                                } else {
                                    line.setParallelPole("0");
                                    //如果选型关联的线1的legendTypeKey有关联的线的选型。高低压之类的我们也需要继承。
                                    if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                        //回 滚
                                    }
                                }
                                line.setState(l.getState());
                            } else {//类型不同的线寻找关联的。
                                Line defaultLine = LineUtils.getInstance().getGline_xianlu(line, project);
                                line.setNum(l.getNum());
                                line.setModuleID(defaultLine.getModuleID());
                                line.setModuleTxt(defaultLine.getModuleTxt());
                                line.setTypes(defaultLine.getTypes());
                                line.setTypeId(defaultLine.getTypeId());
                                line.setType(defaultLine.getType());
                                line.setMethodName(defaultLine.getMethodName());
                                line.setPipe_laying_state("新建");
                                line.setPath(defaultLine.getPath());
                                if (l.getParallelPole() != null && l.getParallelPole().equals("同杆并架")) {
                                    line.setParallelPole(l.getParallelPole());
                                    if (l.getLegendTypeKey() != null && !l.getLegendTypeKey().equals("null") && !l.getLegendTypeKey().isEmpty()) {
                                        line.setLegendTypeKey(l.getLegendTypeKey());
                                    }
                                } else {
                                    line.setParallelPole("0");
                                }
                                line.setState(l.getState());
                            }

                        } else {
                            if (getInstances().isXinJian) {
                                line.setState("新建");

                            } else {
                                line.setState("原有");
                            }
                            Line defaultLine = LineUtils.getInstance().getGline_xianlu(line, project);
                            if (defaultLine != null && defaultLine.getModuleID() != null && !defaultLine.getModuleID().isEmpty()) {
                                line.setNum("1");
                                line.setModuleID(defaultLine.getModuleID());
                                line.setModuleTxt(defaultLine.getModuleTxt());
                                line.setTypes(defaultLine.getTypes());
                                line.setTypeId(defaultLine.getTypeId());
                                line.setType(defaultLine.getType());
                                line.setMethodName(defaultLine.getMethodName());
                                line.setPipe_laying_state("新建");
                                line.setPath(defaultLine.getPath());
                            }
                        }
                        //TODO 保存线
                        if (WHETHER_TO_SHOW_GUIDES) {
                            //drawMyRoute(line);
                            getInstances().getDaoSession().getLineDao().insert(line);
                            returnLineAndPoint = new ReturnLineAndPoint(line, points, ep[0].getId());
                            //TODO 这里是第一次关闭状态所以
                        } else {
                            WHETHER_TO_SHOW_GUIDES = !WHETHER_TO_SHOW_GUIDES;
                        }
                    }
                } else {
                    Line line = new Line();
                    line.setId(null);
                    //line.setLineNumber(1);
                    line.setCid(getUUID());
                    line.setItem_id(project.getId());
                    line.setProjectId(project.getId());
                    String line_names = "线路" + LineUtils.getInstance().getInLinesMaxNumber(project.getId()) + "#";
                    line.setName(line_names);
                    line.setParallelPole("0");
                    line.setLatB(points.getLatitude() + "");
                    line.setLonB(points.getLongitude() + "");
                    line.setLatA(currentMarker.getPosition().getLatitude() + "");
                    line.setLonA(currentMarker.getPosition().getLongitude() + "");
                    line.setStart_id(crrentMartPoints.getUid());
                    //这里肯定是高压电缆线路因为。是连接点和电缆拐点。
                    line.setLegendTypeKey(LineUtils.getInstance().getLineRemarker("高压电缆线路"));
                    line.setEnd_id(points.getUid());
                    line.setLength(lengths + "");
                    line.setBackNum("1");
                    if ((crrentMartPoints.getName().equals("杆塔") || crrentMartPoints.getName().equals("柱上变压器")) && points.getName().equals("杆塔")) {
                        //TODO 这里修改了                        line.setType("架空线路");
                        line.setType("");
                        line.setTypeId("11");

                    } else {
                        line.setType("电缆");
                        line.setTypeId("12");

                    }

                    if (getInstances().isXinJian) {
                        line.setState("新建");

                    } else {
                        line.setState("原有");
                    }
                    Line defaultLine = LineUtils.getInstance().getGline_xianlu(line, project);
                    if (defaultLine != null && defaultLine.getModuleID() != null && !defaultLine.getModuleID().isEmpty()) {
                        line.setNum("1");
                        line.setModuleID(defaultLine.getModuleID());
                        line.setModuleTxt(defaultLine.getModuleTxt());
                        line.setTypes(defaultLine.getTypes());
                        line.setTypeId(defaultLine.getTypeId());
                        line.setType(defaultLine.getType());
                        line.setMethodName(defaultLine.getMethodName());
                        line.setPipe_laying_state("新建");
                        line.setPath(defaultLine.getPath());
                    }
                    //TODO 保存线
                    if (WHETHER_TO_SHOW_GUIDES) {
                        getInstances().getDaoSession().getLineDao().insert(line);
                        returnLineAndPoint = new ReturnLineAndPoint(line, points, ep[0].getId());
                        //TODO 这里是第一次关闭状态所以
                    } else {
                        WHETHER_TO_SHOW_GUIDES = !WHETHER_TO_SHOW_GUIDES;
                        returnLineAndPoint = new ReturnLineAndPoint(null, points, ep[0].getId());

                    }
                }


            }
            points.setDangju(lengths);
            id = getInstances().getDaoSession().getPointDao().insert(points);
            points.setId(id);
        } else {
            points.setName("地形点");
            points.setDangju(lengths);
            id = getInstances().getDaoSession().getPointDao().insert(points);
            points.setId(id);
        }
        if (returnLineAndPoint != null) {
            returnLineAndPoint.setPoint(points);
            return returnLineAndPoint;

        } else {
            return new ReturnLineAndPoint(null, points, ep[0].getId());
        }

    }


    /**
     * 获取UUID
     *
     * @return uuid
     */
    private String getUUID() {
        return UUID.randomUUID() + "";
    }

}
